/**
 * 
 */
package com.cait.dutyfree.dataaccess.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cait.dutyfree.dataaccess.InventoryServiceDAO;
import com.cait.dutyfree.dataaccess.ProductServiceDAO;
import com.cait.dutyfree.exception.DutyFreeException;
import com.cait.dutyfree.pojo.Airline;
import com.cait.dutyfree.pojo.AirlineProduct;
import com.cait.dutyfree.pojo.Category;
import com.cait.dutyfree.pojo.Currency;
import com.cait.dutyfree.pojo.Product;
import com.cait.dutyfree.pojo.Stock;
import com.cait.dutyfree.pojo.SubCategory;
import com.cait.dutyfree.pojo.view.ProductDTO;

/**
 * @author Rasika Kaluwalgoda
 * 
 */
public class InventoryServiceDAOImpl extends HibernateDaoSupport implements
		InventoryServiceDAO {
	private ProductServiceDAO productServiceDAO;

	public void setProductServiceDAO(ProductServiceDAO productServiceDAO) {
		this.productServiceDAO = productServiceDAO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cait.dutyfree.dataaccess.InventoryServiceDAO#createCategory(com.cait
	 * .dutyfree.pojo.Category)
	 */
	public String createCategory(Category category) throws Exception {

		try {
			if (!isAvailableCategoryName(category.getName().trim())) {
				getHibernateTemplate().save(category);
				getHibernateTemplate().flush();
				return "SUCCESS";
			} else {
				logger.warn("Category already available " + category.getName());
				throw new DutyFreeException(2001, new Exception(
						"Category already available"));
			}
		} catch (Exception e) {
			System.out.println(e);
			throw new DutyFreeException(2002, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cait.dutyfree.dataaccess.InventoryServiceDAO#updateCategory(com.cait
	 * .dutyfree.pojo.Category)
	 */
	public String updateCategory(Category category) throws Exception {

		try {
			if (getCategory(category.getId()) != null) {
				getHibernateTemplate().merge(category);
				getHibernateTemplate().flush();
				return "SUCCESS";
			} else {
				logger.warn("Category not available " + category.getName());
				throw new DutyFreeException(2003, new Exception(
						"Category not available"));
			}
		} catch (Exception e) {
			throw new DutyFreeException(2004, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cait.dutyfree.dataaccess.InventoryServiceDAO#getCategory(int)
	 */
	public Category getCategory(int catId) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Category.class);
			criteria.add(Restrictions.eq("id", catId));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			Category category;
			if ((list != null) && (list.size() > 0)) {
				category = (Category) list.get(0);
				return category;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch category by category id ");
			throw new DutyFreeException(2006, new Exception(
					"During fetch category by category id"));
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cait.dutyfree.dataaccess.InventoryServiceDAO#getCategories()
	 */
	public ArrayList<Category> getCategories() throws Exception {

		try {
			ArrayList<Category> categories = new ArrayList<Category>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Category.class);
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					categories.add((Category) list.get(i));
				}
				return categories;
			}
			return null;
		} catch (Exception e) {
			System.out.println(e);
			logger.warn("During fetch all categories ");
			throw new DutyFreeException(1004, new Exception(
					"During fetch all categories"));
		}
	}

	public boolean isAvailableCategoryCode(String code) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Category.class);
			criteria.add(Restrictions.eq("code", code));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0))
				return true;
			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of Category code ");
			throw new DutyFreeException(2007, new Exception(
					"During cross check availability of Category code"));
		}

	}

	public boolean isAvailableCategoryName(String name) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Category.class);
			criteria.add(Restrictions.eq("name", name));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0))
				return true;
			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of Category name ");
			throw new DutyFreeException(2008, new Exception(
					"During cross check availability of Category name"));
		}

	}

	public boolean isAvailableCategoryNameTwice(String name) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Category.class);
			criteria.add(Restrictions.eq("name", name));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				if (list.size() == 2)
					return true;
			}

			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of Category name twice");
			throw new DutyFreeException(2008, new Exception(
					"During cross check availability of Category name twice"));
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cait.dutyfree.dataaccess.InventoryServiceDAO#createSubCategory(com
	 * .cait.dutyfree.pojo.SubCategory)
	 */
	public String createSubCategory(SubCategory category) throws Exception {

		try {
			if (!isAvailableSubCategoryName(category.getName().trim())) {
				getHibernateTemplate().save(category);
				getHibernateTemplate().flush();
				return "SUCCESS";
			} else {
				logger.warn("Sub Category already available "
						+ category.getName());
				throw new DutyFreeException(2009, new Exception(
						"Sub Category already available"));
			}
		} catch (Exception e) {
			System.out.println(e);
			throw new DutyFreeException(2010, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cait.dutyfree.dataaccess.InventoryServiceDAO#updateSubCategory(com
	 * .cait.dutyfree.pojo.SubCategory)
	 */
	public String updateSubCategory(SubCategory category) throws Exception {

		try {
			if (getSubCategory(category.getId()) != null) {
				getHibernateTemplate().merge(category);
				getHibernateTemplate().flush();
				return "SUCCESS";
			} else {
				logger.warn("Sub Category not available " + category.getName());
				throw new DutyFreeException(2011, new Exception(
						"Sub Category not available"));
			}
		} catch (Exception e) {
			throw new DutyFreeException(2012, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cait.dutyfree.dataaccess.InventoryServiceDAO#getSubCategory(int)
	 */
	public SubCategory getSubCategory(int subCatId) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(SubCategory.class);
			criteria.add(Restrictions.eq("id", subCatId));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			SubCategory subCategory;
			if ((list != null) && (list.size() > 0)) {
				subCategory = (SubCategory) list.get(0);
				return subCategory;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch sub category by sub category id ");
			throw new DutyFreeException(2013, new Exception(
					"During fetch sub category by sub category id"));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cait.dutyfree.dataaccess.InventoryServiceDAO#getSubCategories()
	 */
	public ArrayList<SubCategory> getSubCategories() throws Exception {

		try {
			ArrayList<SubCategory> categories = new ArrayList<SubCategory>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(SubCategory.class);
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					categories.add((SubCategory) list.get(i));
				}
				return categories;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all sub categories ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all sub categories"));
		}
	}

	public ArrayList<SubCategory> getSubCategoriesByCategory(int catId)
			throws Exception {

		try {
			Category category = getCategory(catId);
			if (category != null) {
				ArrayList<SubCategory> categories = new ArrayList<SubCategory>();
				DetachedCriteria criteria = DetachedCriteria
						.forClass(SubCategory.class);
				criteria.add(Restrictions.eq("category", category));
				@SuppressWarnings("rawtypes")
				List list = getHibernateTemplate().findByCriteria(criteria);
				if ((list != null) && (list.size() > 0)) {
					for (int i = 0; i < list.size(); i++) {
						categories.add((SubCategory) list.get(i));
					}
					return categories;
				}
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all sub categories ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all sub categories"));
		}
	}

	public boolean isAvailableSubCategoryCode(String code) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(SubCategory.class);
			criteria.add(Restrictions.eq("code", code));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0))
				return true;
			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of sub Category code ");
			throw new DutyFreeException(2007, new Exception(
					"During cross check availability of sub Category code"));
		}
	}

	public boolean isAvailableSubCategoryName(String name) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(SubCategory.class);
			criteria.add(Restrictions.eq("name", name));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0))
				return true;
			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of sub Category name ");
			throw new DutyFreeException(2008, new Exception(
					"During cross check availability of sub Category name"));
		}

	}

	public boolean isAvailableSubCategoryNameTwice(String name)
			throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(SubCategory.class);
			criteria.add(Restrictions.eq("name", name));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				if (list.size() == 2)
					return true;
			}

			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of sub Category name twice");
			throw new DutyFreeException(
					2008,
					new Exception(
							"During cross check availability of sub Category name twice"));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cait.dutyfree.dataaccess.InventoryServiceDAO#createProduct(com.cait
	 * .dutyfree.pojo.Product)
	 */
	public String createProduct(Product product) throws Exception {

		try {
			getHibernateTemplate().merge(product);
			getHibernateTemplate().flush();
			return "SUCCESS";
		} catch (Exception e) {
			System.out.println(e);
			throw new DutyFreeException(2010, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cait.dutyfree.dataaccess.InventoryServiceDAO#updateProduct(com.cait
	 * .dutyfree.pojo.Product)
	 */
	public String updateProduct(Product product) throws Exception {

		try {
			getHibernateTemplate().update(product);
			getHibernateTemplate().flush();
			return "SUCCESS";
		} catch (Exception e) {
			System.out.println(e);
			throw new DutyFreeException(2010, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cait.dutyfree.dataaccess.InventoryServiceDAO#getProduct(int)
	 */
	public Product getProduct(int prdId) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.add(Restrictions.eq("id", prdId));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				return (Product) list.get(0);
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch sub category by sub category id ");
			throw new DutyFreeException(2013, new Exception(
					"During fetch sub category by sub category id"));
		}
	}

	public Product getProduct(String prdCode) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.add(Restrictions.eq("code", prdCode));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				return (Product) list.get(0);
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch sub category by sub category id ");
			throw new DutyFreeException(2013, new Exception(
					"During fetch sub category by sub category id"));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cait.dutyfree.dataaccess.InventoryServiceDAO#getProducts()
	 */
	public ArrayList<Product> getProducts() throws Exception {

		try {
			ArrayList<Product> products = new ArrayList<Product>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					products.add((Product) list.get(i));
				}
				return products;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all products ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all products"));
		}
	}

	public ArrayList<Product> getProducts(Category category) throws Exception {

		try {
			ArrayList<Product> products = new ArrayList<Product>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.add(Restrictions.eq("category", category));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					products.add((Product) list.get(i));
				}
				return products;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.warn("During fetch all products by category");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all products by category"));
		}

	}

	public List<ProductDTO> getProductsDTOByCategoryIdAndAirLineId(
			Integer categoryId, Integer airlineId) {
		Object[] paramsArray = { airlineId, categoryId };
		@SuppressWarnings("unchecked")
		List<Product> products = getHibernateTemplate()
				.find("select distinct p from Product p join p.airlineProducts ad join p.category c where ad.airline.id = ? and c.id = ?",
						paramsArray);
		List<ProductDTO> productsDto = new ArrayList<ProductDTO>();
		for (int i = 0; i < products.size(); i++) {
			ProductDTO pdto = new ProductDTO();
			Product p = products.get(i);

			// set productdto properties
			pdto.setId(p.getId());
			Stock stock = null;
			try {
				stock = this.getStock(p);
				pdto.setAvailableCount(stock.getQuantity());
			} catch (Exception e) {
				e.printStackTrace();
				pdto.setAvailableCount(0);
			}
			List<AirlineProduct> airlineDetails = p.getAirlineProducts();
			for (int j = 0; j < airlineDetails.size(); j++) {
				AirlineProduct ad = airlineDetails.get(j);
				if (ad.getAirline().getId().intValue() == airlineId.intValue()) {
					pdto.setDiscount(ad.getDiscountRate());
					pdto.setPrice(ad.getUnitPrice());
					pdto.setCurrencyCode(ad.getCurrency().getCode());
				}
			}
			pdto.setPrdImageUrl1(p.getPrdImageUrl1());
			pdto.setPrdImageUrl2(p.getPrdImageUrl2());
			pdto.setPrdImageUrl3(p.getPrdImageUrl3());
			pdto.setProductCode(p.getCode());
			pdto.setProductDescription(p.getDescription());
			pdto.setProductName(p.getName());
			pdto.setVendor(p.getVendor());
			pdto.setCategoryName(p.getCategory().getName());
			pdto.setSubCategoryName(p.getSubCategory().getName());
			// end properties

			if (pdto.getAvailableCount() > 0) {
				productsDto.add(pdto);
			}
		}
		return productsDto;
	}

	public List<ProductDTO> getProductDTOBySubcategoryIdAndAirlineId(
			Integer subCategoryId, Integer airlineId) {
		Object[] paramsArray = { airlineId, subCategoryId };
		@SuppressWarnings("unchecked")
		List<Product> products = getHibernateTemplate()
				.find("select distinct p from Product p join p.airlineProducts ad join p.subCategory s where ad.airline.id = ? and s.id = ?",
						paramsArray);
		List<ProductDTO> productsDto = new ArrayList<ProductDTO>();
		for (int i = 0; i < products.size(); i++) {
			ProductDTO pdto = new ProductDTO();
			Product p = products.get(i);

			// set productdto properties
			pdto.setId(p.getId());
			Stock stock = null;
			try {
				stock = this.getStock(p);
				pdto.setAvailableCount(stock.getQuantity());
			} catch (Exception e) {
				e.printStackTrace();
				pdto.setAvailableCount(0);
			}
			List<AirlineProduct> airlineDetails = p.getAirlineProducts();
			for (int j = 0; j < airlineDetails.size(); j++) {
				AirlineProduct ad = airlineDetails.get(j);
				if (ad.getAirline().getId().intValue() == airlineId.intValue()) {
					pdto.setDiscount(ad.getDiscountRate());
					pdto.setPrice(ad.getUnitPrice());
					pdto.setCurrencyCode(ad.getCurrency().getCode());
				}
			}
			pdto.setPrdImageUrl1(p.getPrdImageUrl1());
			pdto.setPrdImageUrl2(p.getPrdImageUrl2());
			pdto.setPrdImageUrl3(p.getPrdImageUrl3());
			pdto.setProductCode(p.getCode());
			pdto.setProductDescription(p.getDescription());
			pdto.setProductName(p.getName());
			pdto.setVendor(p.getVendor());
			pdto.setCategoryName(p.getCategory().getName());
			pdto.setSubCategoryName(p.getSubCategory().getName());
			// end properties

			if (pdto.getAvailableCount() > 0) {
				productsDto.add(pdto);
			}
		}
		return productsDto;
	}

	public ArrayList<Product> getProducts(SubCategory subCategory)
			throws Exception {

		try {
			ArrayList<Product> products = new ArrayList<Product>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			criteria.add(Restrictions.eq("subCategory", subCategory));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					products.add((Product) list.get(i));
				}
				return products;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all products by sub category ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all products by sub category"));
		}

	}

	public Stock getStock(Product product) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(Stock.class);
			criteria.add(Restrictions.eq("product", product));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				return (Stock) list.get(0);
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch stock by product ");
			throw new DutyFreeException(2013, new Exception(
					"During fetch stock by product"));
		}
	}

	public String updateStock(Stock stock) throws Exception {

		try {
			Stock s = getStock(stock.getProduct());
			if (s != null) {
				s.setQuantity(stock.getQuantity());
				getHibernateTemplate().update(s);
				getHibernateTemplate().flush();
			} else {
				getHibernateTemplate().merge(stock);
				getHibernateTemplate().flush();
			}
			return "SUCCESS";
		} catch (Exception e) {
			System.out.println(e);
			throw new DutyFreeException(2010, e);
		}
	}

	public ArrayList<Stock> getStocks() throws Exception {

		try {
			ArrayList<Stock> stocks = new ArrayList<Stock>();
			DetachedCriteria criteria = DetachedCriteria.forClass(Stock.class);
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					stocks.add((Stock) list.get(i));
				}
				return stocks;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all Stocks ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all Stocks"));
		}
	}

	public ArrayList<Airline> getAirlines() throws Exception {

		try {
			ArrayList<Airline> airlines = new ArrayList<Airline>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Airline.class);
			criteria.add(Restrictions.eq("status", true));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					airlines.add((Airline) list.get(i));
				}
				return airlines;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all airlines ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all airlines"));
		}
	}

	public Airline getAirline(int id) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Airline.class);
			criteria.add(Restrictions.eq("id", id));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				return (Airline) list.get(0);
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch airline by id ");
			throw new DutyFreeException(2013, new Exception(
					"During fetch airline by id"));
		}

	}

	public ArrayList<Currency> getCurrencies() throws Exception {

		try {
			ArrayList<Currency> currencies = new ArrayList<Currency>();
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Currency.class);
			criteria.add(Restrictions.eq("status", true));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				for (int i = 0; i < list.size(); i++) {
					currencies.add((Currency) list.get(i));
				}
				return currencies;
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch all currencies ");
			throw new DutyFreeException(2014, new Exception(
					"During fetch all currencies"));
		}
	}

	public Currency getCurrency(int id) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Currency.class);
			criteria.add(Restrictions.eq("id", id));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				return (Currency) list.get(0);
			}
			return null;
		} catch (Exception e) {
			logger.warn("During fetch currency by id ");
			throw new DutyFreeException(2013, new Exception(
					"During fetch currency by id"));
		}

	}

	public boolean isAvailableProductCode(String code) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.add(Restrictions.eq("code", code));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0))
				return true;
			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of Product code ");
			throw new DutyFreeException(2007, new Exception(
					"During cross check availability of Product code"));
		}
	}

	public boolean isAvailableProductName(String name) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.add(Restrictions.eq("name", name));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0))
				return true;
			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of Product name ");
			throw new DutyFreeException(2007, new Exception(
					"During cross check availability of Product name"));
		}
	}

	public boolean isAvailableProductNameTwice(String name) throws Exception {

		try {
			DetachedCriteria criteria = DetachedCriteria
					.forClass(Product.class);
			criteria.add(Restrictions.eq("name", name));
			@SuppressWarnings("rawtypes")
			List list = getHibernateTemplate().findByCriteria(criteria);
			if ((list != null) && (list.size() > 0)) {
				if (list.size() == 2)
					return true;
			}

			return false;
		} catch (Exception e) {
			logger.warn("During cross check availability of Product name twice");
			throw new DutyFreeException(2008, new Exception(
					"During cross check availability of Product name twice"));
		}
	}

}
